#!/usr/bin/env python

from celtuce.accessor import Accessor

import time

from celtuce.help import logging
logging.getLogger().setLevel(logging.LEVEL_DEBUG)

URL = 'tcp://127.0.0.1:14300'

TABLE = 'table_test'

def runTest() :

    def feedData(data_size, block_size=1048576) :
        data_chunk = 'A' * block_size
        len_data_chunk = len(data_chunk)
        sum = 0
        index = 0
        time_start = time.time()
        try :
            while sum < data_size :
                key = '-'.join(('hello',str(index)))
                value = '-'.join(('world',str(index),data_chunk))
                response = accessor.insert(TABLE, key, value)
                if not response.isOk() :
                    raise RuntimeError, response.error
                index += 1
                sum += len_data_chunk

                if index > 0 and (0 == (index % 10000)) :
                    time_last = time.time() - time_start
                    tps = index / time_last
                    bw  = sum / time_last / 1048576
                    print 'insert %d records (block-size:%.2f KB, volumn:%.2f MB) last %.2f seconds, TPS=%.2f BW=%.2f MB/s ...' % (index, block_size/1024.0, sum/1048576.0, time_last, tps, bw)

        finally :
            time_last = time.time() - time_start
            tps = index / time_last
            bw  = sum / time_last / 1048576
            print 'insert %d records (block-size:%.2f KB, volumn:%.2f MB) last %.2f seconds, TPS=%.2f BW=%.2f MB/s' % (index, block_size/1024.0, sum/1048576.0, time_last, tps, bw)
        max_index = index 
        
        sum = 0
        index = 0
        time_start = time.time()
        try :
            for index in xrange(max_index) :
                key = '-'.join(('hello',str(index)))
                value = '-'.join(('world',str(index),data_chunk))
                response = accessor.find(TABLE, key)
                if not response.isOk() :
                    raise RuntimeError, response.error
                print response.body
                if response.body[1] != value :
                    print 'ERROR : GOT %s, WANT %s' % (response.body, value)
                assert response.body[1] == value
                sum += len(value)

                if index > 0 and (0 == (index % 10000)) :
                    time_last = time.time() - time_start
                    tps = index / time_last
                    bw  = sum / time_last / 1048576
                    print 'find   %d records (block-size:%.2f KB, volumn:%.2f MB) last %.2f seconds, TPS=%.2f BW=%.2f MB/s ...' % (index, block_size/1024.0, sum/1048576.0, time_last, tps, bw)

            index = max_index
        finally :
            time_last = time.time() - time_start
            tps = index / time_last
            bw  = sum / time_last / 1048576
            print 'find   %d records (block-size:%.2f KB, volumn:%.2f MB) last %.2f seconds, TPS=%.2f BW=%.2f MB/s' % (index, block_size/1024.0, sum/1048576.0, time_last, tps, bw)
            
        ## feedData()

    accessor = Accessor( URL )
    try :
        accessor.insert(TABLE,'hello', 'world')
        key = 'hello-not-found'
        response = accessor.find(TABLE, key)
        assert response.isOk()
        _key,_value = response.body
        assert _key == key
        assert _value is None

        response = accessor.find(TABLE, 'hello')
        assert response.isOk()
        _key,_value = response.body
        assert _key == 'hello'
        assert _value == 'world'

        # test performance
        test_times = 1000
        print ' - test find %d times ...' % (test_times,)
        time_start = time.time()
        for i in xrange(test_times) :
            response = accessor.find(TABLE, 'hello')
            assert response.isOk()
            _key,_value = response.body
            assert _key == 'hello'
            assert _value == 'world'
        time_last = time.time() - time_start
        tps = test_times / time_last
        print 'test find() %d times last %.2f seconds, tps=%.2f' % (test_times, time_last, tps)

        K = 1024
        M = 1024 * 1024
        G = 1024 * 1024 * 1024

        block_size = 1 * K
        data_size = 10 * G


        feedData(data_size, block_size)

    finally :
        accessor.shutdown()

    ## runTest()

if  __name__ == '__main__' :
    runTest()

        ## removedirs


